/**
 * 获取当前时间戳
 * @param type
 */
export function getTimestamp(type: "long" | "short" = 'short'): number {
    let time = (new Date()).getTime()
    if (type === 'long') {
        return time
    }
    return parseInt((time / 1000).toFixed(0))
}


/**
 * 格式化时间
 * @param time
 */
export function getTimeInfo(time?: Date | number | string): { [key: string]: string } {
    let date: Date;
    if (time instanceof Date) {
        date = time
    } else if (typeof time === 'number') {
        date = time.toString().length === 10 ? new Date(time * 1000) : new Date(time)
    } else if (typeof time === 'string') {
        date = new Date(time)
    } else {
        date = new Date()
    }
    const _Year: string = date.getFullYear().toString()
    //截取_Year后两位

    const _Month: number = (date.getMonth() + 1)
    const _Date: number = date.getDate()
    const _Hours: number = date.getHours()
    const _Minutes: number = date.getMinutes()
    const _Seconds: number = date.getSeconds()
    const _Week: number = date.getDay();
    return {
        Y: _Year,//年
        y: _Year.slice(-2),//年
        m: timeFormatComplement(_Month),// 月
        d: timeFormatComplement(_Date),// 日
        H: timeFormatComplement(_Hours),// 时
        h: timeFormatComplement(_Hours > 12 ? _Hours - 12 : _Hours),// 时
        i: timeFormatComplement(_Minutes),//分
        s: timeFormatComplement(_Seconds),//秒
        w: (_Week + 1).toString(),//我就喜欢星期从1开始
        zw: ['日', '一', '二', '三', '四', '五', '六'][_Week]
    }
}

/**
 * 格式化时间戳
 */
export function timestampFormat(format: string = 'Y-m-d H:i:s', time?: Date | number | string): string {
    const times = getTimeInfo(time)
    for (let key in times) {
        format = format.replace(new RegExp(`(${key})`, 'g'), times[key]);
    }
    return format
}


/**
 * 解析时间
 */
export function analysisTime(time: number | string | Date): null | { obj: Date, timestamp: number, microtime: number } {
    if (time) {
        let date: Date;
        if (time instanceof Date) {
            date = time
        } else if (typeof time === 'number') {
            date = time.toString().length === 10 ? new Date(time * 1000) : new Date(time)
        } else {
            date = new Date(time)
        }
        const microtime = date.getTime()
        return {
            obj: date,
            timestamp: parseInt((date.getTime() / 1000).toFixed(0)),
            microtime,
        }
    }
    return null
}


/**
 * 秒转为时间
 * @param time
 * @constructor
 */
export function Sec2Time(time: number): string {
    let value:{years: number, days: number, hours: number, minutes: number, seconds: number} = {years: 0, days: 0, hours: 0, minutes: 0, seconds: 0}
    if (time >= 31556926) {
        value.years = Math.floor(time / 31556926);
        time = (time % 31556926);
    }
    if (time >= 86400) {
        value.days = Math.floor(time / 86400);
        time = (time % 86400);
    }
    if (time >= 3600) {
        value.hours = Math.floor(time / 3600);
        time = (time % 3600);
    }
    if (time >= 60) {
        value.minutes = Math.floor(time / 60);
        time = (time % 60);
    }
    value.seconds = Math.floor(time);
    return value.years + '年' + value.days + '天 ' + value.hours + '小时' + value.minutes + '分' + value.seconds + '秒';
}


/**
 * 时间补位
 * @param value
 */
function timeFormatComplement(value: number): string {
    return ((value < 10) ? ('0' + value) : value).toString();
}
